/**
 * 
 */
package com.wordpress.music2dot0.server;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Server that handles the general group list and the incoming connections
 * 
 * @author raf
 */
public class LoginServer {
	/**
	 * General logger for the music server
	 */
	public static Logger log = Logger.getLogger("MusicServer");
	private static final int PORT = 5000;
	private static GroupServerList groups = new GroupServerList();

	private static void acceptConnections() {
		try {
			final ServerSocket serverSocket = new ServerSocket(PORT);
			Socket clientSocket;
			log.info("LoginServer running, awaiting connections");
			while ((clientSocket = serverSocket.accept()) != null) {
				log.info("New connection: " + clientSocket.getInetAddress());
				final Connection runnable = new Connection(clientSocket);
				final Thread thread = new Thread(runnable);
				thread.start();
			}
		} catch (final IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * Retrieves the general group server list
	 * 
	 * @return the group server list (all locations)
	 */
	public static GroupServerList getGroups() {
		return LoginServer.groups;
	}

	/**
	 * Adds some groups and then waits for connections
	 * 
	 * @param args
	 *            parameters (not used)
	 */
	public static void main(String[] args) {
		log.setLevel(Level.FINEST);
		groups.add(new GroupServer("CW-Radio", 50.863361, 4.679135));
		groups.add(new GroupServer("KUL", 50.877972, 4.699901));
		groups.add(new GroupServer("China", 26.0, 117.0));
		groups.add(new GroupServer("India", 10.0, 77.0));
		acceptConnections();
	}
}
